package com.wxy.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wxy.domain.user;
import com.wxy.service.registerService;

import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/boot2")
public class SecurityController {
	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private registerService registerService;
	@RequestMapping("register")
	@ResponseBody
	public String register(HttpServletRequest request) {
		//获取到前端数据
		String registerMessage=request.getParameter("data");
		user registerUser=JSONUtil.toBean(registerMessage,user.class);
		//默认的权限为consumer
		registerUser.setRole("consumer");
		//将registerUser中的password加密
		String EncodePassword=passwordEncoder.encode(registerUser.getPassword());
		registerUser.setPassword(EncodePassword);
		//将注册的数据添加到数据库中
		boolean isOk=registerService.insertUser(registerUser);
		if(isOk==true)
			return "注册成功，请返回登录...";
		return "用户名已存在，请重试...";
	}
	@RequestMapping("/hello")
	public String hello() {
		return "/html/hello.html";
	}
	@PreAuthorize("hasRole('boss')")
	@RequestMapping("/boss")
	public String boss() {
		return "/html/boss.html";
	}
	@PreAuthorize("hasRole('consumer')")
	@RequestMapping("/consumer")
	public String consumer() {
		return "/html/consumer.html";
	}
}
